﻿using System;
using System.IO;
using DiskUsage.CoreLib;

namespace DiskUsage
{
    class DirectoryListPresentation
    {
        static ISizeStore s_sizeStore = SizeStoreFactory.GetSizeStore();

        public string Name { get; private set; }
        public string Icon { get; private set; }
        public DateTime DateModified { get; private set; }
        public String Type { get; private set; }
        public long Size { get; private set; }
        public string Path { get; private set; }
        public DirectoryInfo DirectoryInfo { get; private set; }
        public FileInfo FileInfo { get; private set; }

        public DirectoryListPresentation(string path)
        {
            InitPath(path);
        }

        public DirectoryListPresentation(FileInfo file)
        {
            InitFileInfo(file);
        }

        public DirectoryListPresentation(DirectoryInfo directory)
        {
            InitDirInfo(directory);
        }

        private void InitPath(string path)
        {
            var type = PathUtils.GetPathType(path);
            if (type == PathType.Invalid)
                throw new ArgumentException("Path does not exist: " + path);

            if (type == PathType.File)
                InitFileInfo(new FileInfo(path));
            else if (type == PathType.Directory)
                InitDirInfo(new DirectoryInfo(path));
            else throw new ArgumentException("Path type not understood: " + path);
        }

        private void InitFileInfo(FileInfo file)
        {
            Name = file.Name;
            Icon = "Resources/file.png";
            DateModified = file.LastWriteTime;
            Type = "File";
            Path = file.FullName;
            Size = s_sizeStore.GetSize(Path);
            FileInfo = file;
        }

        private void InitDirInfo(DirectoryInfo dir)
        {
            Name = dir.Name;
            Icon = "Resources/folder.png";
            DateModified = dir.LastWriteTime;
            Type = "Directory";
            Path = dir.FullName;
            Size = s_sizeStore.GetSize(Path);
            DirectoryInfo = dir;
        }
    }
}
